﻿/* Copyright© 2000 - 2020 SuperMap Software Co.Ltd. All rights reserved.
 * This program are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
 (function () {
    var r = new RegExp("(^|(.*?\\/))(include-mapboxgl\.js)(\\?|$)"),
        s = document.getElementsByTagName('script'),
        targetScript;
    for (var i = 0; i < s.length; i++) {
        var src = s[i].getAttribute('src');
        if (src) {
            var m = src.match(r);
            if (m) {
                targetScript = s[i];
                break;
            }
        }
    }

    function inputScript(url) {
        var script = '<script type="text/javascript" src="' + url + '"><' + '/script>';
        document.writeln(script);
    }

    function inputCSS(url) {
        var css = '<link rel="stylesheet" href="' + url + '">';
        document.writeln(css);
    }

    function inArray(arr, item) {
        for (i in arr) {
            if (arr[i] == item) {
                return true;
            }
        }
        return false;
    }

    function supportES6() {
        var code = "'use strict'; class Foo {}; class Bar extends Foo {};";
        try {
            (new Function(code))();
        } catch (err) {
            return false;
        }
        if (!Array.from) {
            return false;
        }
        return true;
    }

    //加载类库资源文件
    function load() {
        var includes = (targetScript.getAttribute('include') || "").split(",");
        var excludes = (targetScript.getAttribute('exclude') || "").split(",");
        if (!inArray(includes, 'mapbox-gl-enhance') && !inArray(excludes, 'mapbox-gl')) {
            // inputCSS('static/mapbox-gl/css/mapbox-gl.min.css');
            inputCSS('static/mapbox-gl/css/mapbox-gl.min.css');
            inputScript('static/mapbox-gl/mapbox-gl.min.js');
        }
        if (inArray(includes, 'mapbox-gl-enhance')) {
          inputCSS("static/mapbox-gl/mapbox-gl-js-enhance/mapbox-gl-enhance.css");
          inputScript("static/mapbox-gl/mapbox-gl-enhance-sp.js");
        }
        if (inArray(includes, 'MapboxDraw')) {
            inputCSS("static/mapbox-gl/mapbox-gl-draw/mapbox-gl-draw.css");
            inputScript("static/mapbox-gl/mapbox-gl-draw/mapbox-gl-draw.js");
        }
        if (inArray(includes, 'compare')) {
            inputCSS(
                'static/mapbox-gl/mapbox-gl-compare/mapbox-gl-compare.css'
            );
            inputScript(
                'static/mapbox-gl/mapbox-gl-compare/mapbox-gl-compare.js'
            );
        }
        if (inArray(includes, 'mapv')) {
          inputScript("static/mapbox-gl/js/mapv.min.js");
        }
        if (inArray(includes, 'echarts')) {
            inputScript('static/echarts/js/echarts.min.js');
            inputScript('static/mapbox-gl/echartsLayer/EchartsLayer.min.js');
            inputScript('static/mapbox-gl/echarts-liquidfill/echarts-liquidfill.min.js');
        }
        if (inArray(includes, 'echarts-vue')) {
            inputScript('static/echarts/js/echarts.min.js');
            inputScript("static/mapbox-gl/js/vue-echarts.min.js");
            inputScript("static/mapbox-gl/echarts-liquidfill/echarts-liquidfill.min.js");
            inputScript("static/mapbox-gl/echartsLayer/EchartsLayer.min.js");
        }
        if (inArray(includes, 'three')) {
            inputScript("static/mapbox-gl/js/three.js/92/three.min.js");
        }
        if (inArray(includes, 'xlsx')) {
            inputScript('static/mapbox-gl/js/xlsx.core.min.js');
        }
        if (inArray(includes, 'proj4')) {
            inputScript('static/mapbox-gl/js/proj4.js');
        }
        if (inArray(includes, 'shapefile')) {
          inputScript("static/mapbox-gl/js/shapefile.js");
        }
        if (inArray(includes, 'ant-design-vue')) {
            inputCSS("static/mapbox-gl/ant-design-vue/antd.min.css");
            inputScript("static/mapbox-gl/ant-design-vue/antd.min.js");
        }
        if (inArray(includes, 'antd')) {
            inputCSS("static/mapbox-gl/ant-design-vue/antd.min.css");
            inputScript("static/mapbox-gl/js/moment.min.js");
            inputScript("static/mapbox-gl/ant-design-vue/antd.min.js");
        }
        if (inArray(includes, 'deck')) {
            inputScript("static/mapbox-gl/js/deck.gl.min.js");
        }
        if (inArray(includes, 'jsonsql')) {
          inputScript("static/mapbox-gl/js/jsonsql.min.js");
        }
        if (inArray(includes, 'geostats')) {
            inputScript("static/mapbox-gl/js/geostats.min.js");
        }
        if (inArray(includes, 'canvg')) {
            inputScript("static/mapbox-gl/js/umd.min.js");
        }
        if (inArray(includes, 'convert')) {
            inputScript("static/mapbox-gl/js/xml-js.min.js");
        }
        if (inArray(includes, 'LoaderSupport')) {
            inputScript("static/mapbox-gl/LoaderSupport/LoaderCommons.js");
            inputScript("static/mapbox-gl/LoaderSupport/LoaderBuilder.js");
            inputScript("static/mapbox-gl/LoaderSupport/LoaderWorkerSupport.js");
        }
        if (inArray(includes, 'OBJLoader')) {
            inputScript("static/mapbox-gl/OBJLoader.min.js");
        }
        if (inArray(includes, 'OBJLoader2')) {
            inputScript("static/mapbox-gl/js/OBJLoader2.min.js");
        }
        if (inArray(includes, 'MTLLoader')) {
            inputScript("static/mapbox-gl/js/MTLLoader.js");
        }
        if (inArray(includes, 'GLTFLoader')) {
            inputScript("static/mapbox-gl/js/GLTFLoader.js");
        }
        if (inArray(includes, 'echarts-gl')) {
            inputScript("static/mapbox-gl/echarts-gl/echarts-gl.min.js");
        }

        // iclient
        if (!inArray(excludes, 'iclient-mapboxgl')) {
          if (supportES6()) {
              inputScript("static/mapbox-gl/iclient/iclient-mapboxgl-es6.js");
          } else {
              inputScript("static/mapbox-gl/iclient/iclient-mapboxgl.min.js");
          }
        }
        if (!inArray(excludes, 'iclient-mapboxgl-css')) {
            inputCSS("static/mapbox-gl/iclient/iclient-mapboxgl.min.css");
        }
        if (inArray(includes, 'iclient-mapboxgl-vue')) {
            inputCSS("static/mapbox-gl/iclient/iclient-mapboxgl-vue.min.css");
            inputScript("static/mapbox-gl/iclient/iclient-mapboxgl-vue.min.js");
        }

        if (inArray(includes, 'html2canvas')) {
            inputScript("static/js/html2canvas.min.js");
        }



    }

    load();
    window.isLocal = false;
    window.server = document.location.toString().match(/file:\/\//) ? "http://localhost:8090" : document.location.protocol + "//" + document.location.host;
})();
